import Mock from 'mockjs'
import usersData from './users_data'
let _Users = usersData

export default {
  list: config => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve([200, {
          users: _Users,
        }])
      }, 500)
    })
  },
  ratelist: config => {

    let newArr = [];
    for (let i = 0; i < _Users.length; i++) {
      newArr[i] = _Users[i]
    }

    newArr.sort(function (a, b) {
      return b.rate - a.rate
    });

    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve([200, {
          users: newArr,
        }])
        newArr = null;
      }, 500)
    })
  },
  melist: config => {

    var newArr = _Users.filter(function (user) {

      var firseName = user.name.substr(0, 1);
      //console.log(firseName)
      return firseName == "丁" || firseName == "夏" || firseName == "黄" || firseName == "杜" || firseName == "任" || firseName == "何" || firseName == "汤"
    });

    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve([200, {
          users: newArr,
        }])
        newArr = null;
      }, 500)
    })
  },
  addComment: config => {
    _Users.unshift({
      id: config.id,
      name: config.name,
      content: config.content,
      img: config.img,
      like: 0,
      rate: config.rate,
      discuss: []
    })
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve([200, {
          code: 200,
          msg: '添加成功'
        }])
      })
    })
  },
  addReply: config => {

    for (var i = 0; i < _Users.length; i++) {
      if (_Users[i].id == config.id) {
        _Users[i].discuss.unshift({ name: "myName", data: config.data });
        //console.log(_Users[i].discuss);
        var myIndex = i;
      }
    }
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve([200, {
          code: 200,
          index: myIndex
        }])
      })
    })
  },
  changeLike: config => {
    console.log(config.id)
    for (var i = 0; i < _Users.length; i++) {
      if (_Users[i].id == config.id) {
        if (config.bool) {
          _Users[i].like++;
        } else {
          _Users[i].like--;
        }
      }
    }
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve([200, {
          code: 200
        }])
      })
    })
  }
}